package com.example.projectsharedspace;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

public class MessageAdapter extends BaseAdapter implements OnClickListener {
	private Context context;
	
	private List<Message> messageList;
	
	public MessageAdapter(Context context, List<Message> messageList) {
		this.context = context;
		this.messageList = messageList;
	}
	
	public int getCount() {
		return messageList.size();
	}

	public Object getItem(int position) {
		return messageList.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup viewGroup) {
		Message entry = messageList.get(position);
		if(convertView == null) {
			LayoutInflater inflater = (LayoutInflater) context.
					getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.message_row, null);
		}
		
		
		TextView user = (TextView) convertView.findViewById(R.id.user);
		user.setText(entry.getUser().getUN());
		
		TextView msg = (TextView) convertView.findViewById(R.id.message);
		msg.setText(entry.getShortMsg());
		
		TextView timedate = (TextView) convertView.findViewById(R.id.timeDate);
		timedate.setText(entry.getTD().getTime().toString());
		
		// Set the onClick listener on this button.
		
		Button btnRemove = (Button) convertView.findViewById(R.id.bRemove);
		btnRemove.setFocusableInTouchMode(false);
		btnRemove.setFocusable(false);
		btnRemove.setOnClickListener(this);
		
		btnRemove.setTag(entry);
		return convertView;
	}
	
	public void onClick(View view) {
		Message entry = (Message) view.getTag();
		messageList.remove(entry);
		notifyDataSetChanged();
	}
	
	private void showDialog(Message entry) {
		
	}

}
